package com.pet.dao;

import com.pet.bean.Owner;
import com.pet.bean.Petstore;
import com.pet.util.DataSourcesTool;

import javax.xml.crypto.Data;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OwnerDao {
    //宠物主人登录的操作
    public Owner login_Owner(Connection con, String name, String pw) throws SQLException {
        String sql = "select * from owner where name=? and password=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, name);
        ps.setString(2, pw);
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()) {
            int owner_id = resultSet.getInt("owner_id");
            String name1 = resultSet.getString("name");
            String password = resultSet.getString("password");
            double wealth = resultSet.getDouble("wealth");
            int status = resultSet.getInt("status");
            Owner owner = new Owner(owner_id, name1, password, wealth, status);
            return owner;
        }
        return null;
    }
    //宠物主人注册的操作
    public boolean insert_Owner(Connection con, Owner owner) throws SQLException {
        String sql = "insert into Owner(name,password,wealth) values (?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, owner.getName());
        preparedStatement.setString(2, owner.getPassword());
        preparedStatement.setDouble(3, owner.getWealth());
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            return true;
        }
        return false;
    }
    //宠物主人修改的操作
    public boolean update_Owner(Connection con, Owner owner) throws SQLException {
        String sql = "update Owner set name=? password=? wealth=? where owner_id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, owner.getName());
        preparedStatement.setString(2, owner.getPassword());
        preparedStatement.setDouble(3, owner.getWealth());
        preparedStatement.setInt(4, owner.getOwner_id());
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            return true;
        }
        return false;
    }
    //宠物主人删除注销的操作
    public boolean delete_Owner(Connection con, int id) throws SQLException {
        String sql = "update owner set status=0 where owner_id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            return true;
        }
        return false;
    }
    //修改宠物主人的金额操作（买宠物）
    public void update_OwnerWealth1(Connection con, int owner_id, double wealth) throws SQLException {
            String sql = "update Owner set wealth=wealth-? where owner_id=?";
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setDouble(1, wealth);
            preparedStatement.setInt(2, owner_id);
            preparedStatement.executeUpdate();
    }
    //修改宠物主人的金额操作（卖宠物）
    public void update_OwnerWealth2(Connection con, int owner_id, double wealth) throws SQLException {
        String sql = "update Owner set wealth=wealth+? where owner_id=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setDouble(1, wealth);
        ps.setInt(2, owner_id);
        ps.executeUpdate();
    }
    //查看主人列表
    public List<Owner> selectOwner(Connection con) throws SQLException {
        List<Owner> list = new ArrayList<>();
        String sql = "select * from owner where status=0";
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()) {
            int owner_id = resultSet.getInt("owner_id");
            String name = resultSet.getString("name");
            String password = resultSet.getString("password");
            double wealth = resultSet.getDouble("wealth");
            int status = resultSet.getInt("status");
            Owner owner = new Owner(owner_id, name, password, wealth, status);
            list.add(owner);
        }
        return list;
    }
    //根据owner_id查询主人
    public Owner select_OwnerByID(Connection con, int o_id) throws SQLException {
        String sql = "select * from owner where owner_id=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, o_id);
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()) {
            int owner_id = resultSet.getInt("owner_id");
            String name = resultSet.getString("name");
            String password = resultSet.getString("password");
            double wealth = resultSet.getDouble("wealth");
            int status = resultSet.getInt("status");
            Owner owner = new Owner(owner_id, name, password, wealth, status);
            return owner;
        }
        return null;
    }
    //更新主人名字
    public boolean update_OwnerName(Connection con, int id, String name) throws SQLException {
        String sql = "update owner set name=? where owner_id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setInt(2, id);
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            return true;
        }
        return false;
    }
    //更新主人密码
    public boolean update_OwnerPw(Connection con, int id, String password) throws SQLException {
        String sql = "update owner set password=? where owner_id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, password);
        preparedStatement.setInt(2, id);
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            return true;
        }
        return false;
    }
    //更新主人财富
    public boolean update_OwnerWea(Connection con, int id, double wealth) throws SQLException {
        String sql = "update owner set wealth=? where owner_id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setDouble(1, wealth);
        preparedStatement.setInt(2, id);
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            return true;
        }
        return false;
    }
}
